package com.flyqiu.form.test.no.spring;

import com.flyqiu.form.api.FlyQiuForm;
import com.flyqiu.form.api.FlyQiuFormContext;
import com.flyqiu.form.api.event.FlyQiuTableDataEventManage;
import com.flyqiu.form.api.event.FlyQiuTableEventManage;
import com.flyqiu.form.api.service.FlyQiuTableConfigService;
import com.flyqiu.form.api.store.FlyQiuFormCoreService;
import com.flyqiu.form.core.DefaultFlyQiuFormConfig;
import com.flyqiu.form.core.DefaultFlyQiuFormImpl;
import com.flyqiu.form.core.event.GlobalFlyQiuTableDataEvent;
import com.flyqiu.form.core.event.GlobalFlyQiuTableEvent;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

public class BaseForm {
    protected FlyQiuFormCoreService flyQiuCoreForm;

    protected FlyQiuForm flyQiuForm;
    protected FlyQiuTableConfigService flyQiuTableConfigService;

    public BaseForm() {
        initFormCore();
    }

    private void initFormCore() {
        DataSource dataSource = initMysqlDataSource();
        flyQiuForm = new DefaultFlyQiuFormImpl(dataSource);
        flyQiuForm.init(new DefaultFlyQiuFormConfig());
        FlyQiuFormContext context = flyQiuForm.getContext();
        FlyQiuTableEventManage formEventManage = context.getFormEventManage();
        formEventManage.register(new GlobalFlyQiuTableEvent(context));
        FlyQiuTableDataEventManage formDataEventManage = context.getFormDataEventManage();
        formDataEventManage.register(new GlobalFlyQiuTableDataEvent());
        flyQiuCoreForm = flyQiuForm.getCoreFormService();
        flyQiuTableConfigService = flyQiuForm.getTableConfigService();
//        System.out.println("表单引擎：" + flyQiuCoreForm);
    }

    private DataSource initMysqlDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/fly_qiu_form?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("startR00T");
        dataSource.setIdleTimeout(60000);
        dataSource.setAutoCommit(false);
        dataSource.setMaximumPoolSize(5);
        dataSource.setMinimumIdle(1);
        dataSource.setMaxLifetime(60000 * 10);
        dataSource.setConnectionTestQuery("SELECT 1");
        return dataSource;
    }
}
